package com.heba.greenmulu.scan;

import android.content.res.Resources;
import android.os.Bundle;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.heba.adapter.FmPagerAdapter;
import com.heba.config.CommonConfig;
import com.heba.fragment.scan.FeatureProductFragment;
import com.heba.fragment.scan.FeatureUserFragment;
import com.heba.greenmulu.BaseActivity;
import com.heba.greenmulu.R;
import com.heba.utils.IndicatorLineUtil;

import java.util.ArrayList;
import java.util.Objects;

/**
 * Feataure Page
 * Created by jason on 2020/10/02.
 */
public class ItemFeatureActivity extends BaseActivity {

    /**
     * tablayout
     */
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FmPagerAdapter pagerAdapter;
    private int selectIndex = 0;
    private ConstraintLayout bottomLine1;
    private TextView alertLB;
    /**
     * fragment
     */
    private FeatureProductFragment featureProductFragment;
    private FeatureUserFragment featureUserFragment;
    private ArrayList<Fragment> fragments = new ArrayList<>();

    private String title;
    private String featureID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_feature);
        initTopbar();
        findViewById();
        initView();
        initFragments();
        setCustomTabView();
    }

    @Override
    protected void findViewById() {
        bottomLine1 = findViewById(R.id.bottom_line_1);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);

    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            featureID = bundle.getString(CommonConfig.FEATURE_ID, "");
            title = bundle.getString(CommonConfig.PRODUCT_SEARCH_TITLE, "");
        }

        txt_title.setText(title);
    }

    /**
     * initFragments
     */
    private void initFragments() {
        featureProductFragment = FeatureProductFragment.newInstance(featureID);
        featureUserFragment = FeatureUserFragment.newInstance(featureID);
        fragments.add(featureProductFragment);
        fragments.add(featureUserFragment);
    }

    /**
     * 设置自定义tab
     */
    private void setCustomTabView() {
        /**
         * 添加tab
         */
        for (int i = 0; i < 2; i++) {
            tabLayout.addTab(tabLayout.newTab());
        }

        /**
         * 关联viewpager,设置adapter
         */
        tabLayout.setupWithViewPager(viewPager, false);
        pagerAdapter = new FmPagerAdapter(fragments, getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(3);

        /**
         * 设置tab文字,必须在关联viewpager之后设置，否则文字不显示
         */
        Objects.requireNonNull(tabLayout.getTabAt(0)).setCustomView(getTabItemView(R.drawable.image_food_selector));
        Objects.requireNonNull(tabLayout.getTabAt(1)).setCustomView(getTabItemView(R.drawable.image_friends_selector));

        /**
         * 设置指示线长度间隔(单位dp)
         */
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                IndicatorLineUtil.setIndicator(tabLayout, 0, 0);
            }
        });
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tabLayout.getSelectedTabPosition();
                Resources resources = getResources();
                DisplayMetrics dm = resources.getDisplayMetrics();
                int width3 = dm.widthPixels;
                TranslateAnimation translateAnimation1 = new TranslateAnimation(selectIndex*(width3/2),position*(width3/2),0,0);
                translateAnimation1.setDuration(300);
                translateAnimation1.setFillEnabled(true);//使其可以填充效果从而不回到原地
                translateAnimation1.setFillAfter(true);//不回到起始位置
                bottomLine1.startAnimation(translateAnimation1);


                translateAnimation1.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        selectIndex = tabLayout.getSelectedTabPosition();



                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 获取itemview
     *
     * @param icon
     * @return
     */
    private View getTabItemView(int icon) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_tab_layout, null);
        ImageView iv_icon = view.findViewById(R.id.iv_icon);
        iv_icon.setImageResource(icon);
        return view;
    }
}
